#include "superblock.h"

Superblock::Superblock(char * buf)
{

    rozmiarBloku=1024 << char4int(buf,24);
    liczbaBlokow=char4int(buf,4);
    liczbaWolnychBlokow=char4int(buf,12);
    liczbaBlokowNaGrupe = char4int(buf,32);
    liczbaGrup1 =  qCeil ( (double)liczbaBlokow / liczbaBlokowNaGrupe);
    liczbaInodow = char4int(buf,0);
    liczbaWolnychInodow = char4int(buf,16);
    liczbaInodowNaGrupe = char4int(buf,40);
    liczbaGrup2 =  qCeil ( (double)liczbaInodow / liczbaInodowNaGrupe);
    s_rev_level = char4int(buf,76);
    s_minor_rev_level =char2int(buf,62);
    if(s_rev_level >= 1)
    {
        s_feature_compat= char4int(buf,92);
        s_feature_incompat= char4int(buf,96);
        s_feature_ro_compat= char4int(buf,100);
    }
}
void Superblock::stat()
{

    c<<"\tRozmiar pojedyńczego bloku: "<<rozmiarBloku<<e;
    c<<"\tLiczba wszystkich bloków: "<<liczbaBlokow<<e;
    c<<"\tLiczba wszystkich wolnych bloków: "<<liczbaWolnychBlokow<<e;
    c<<"\tLiczba bloków na grupe: "<<liczbaBlokowNaGrupe<<e;
    c<<"\tLiczba wszystkich i-węzłów: "<<liczbaInodow<<e;
    c<<"\tLiczba wolnych i-węzłów: "<<liczbaWolnychInodow<<e;
    c<<"\tLiczba i-węzłów na grupe: "<< liczbaInodowNaGrupe<<e;
    c<<"\tLiczba grup (na podstawie liczby bloków): "<< liczbaGrup1<<e;
    c<<"\tLiczba grup (na podstawie liczby i-węzłów): "<< liczbaGrup2<<e<<e;
    c<<"\tWersja: "<< s_rev_level<<"."<<s_minor_rev_level<<e;
    if(s_rev_level >= 1)
    {
        if(s_feature_compat&0x0001 ) c<<"\tPreallocate some number of (contiguous?) blocks (see byte 205 in the superblock) to a directory when creating a new one (to reduce fragmentation?)"<<e;
        if(s_feature_compat&0x0002 ) c<<"\tAFS server inodes exist "<<e;
        if(s_feature_compat&0x0004 ) c<<"\tFile system has a journal (Ext3) "<<e;
        if(s_feature_compat&0x0008 ) c<<"\tInodes have extended attributes "<<e;
        if(s_feature_compat&0x0010 ) c<<"\tFile system can resize itself for larger partitions "<<e;
        if(s_feature_compat&0x0020 ) c<<"\tDirectories use hash index"<<e;
        if(s_feature_incompat&0x0001 	 ) c<<"\tCompression is used "<<e;
        if(s_feature_incompat& 0x0002 	 ) c<<"\tDirectory entries contain a type field "<<e;
        if(s_feature_incompat&0x0004 	 ) c<<"\tFile system needs to replay its journal "<<e;
        if(s_feature_incompat&0x0008 	 ) c<<"\tFile system uses a journal device"<<e;
        if(s_feature_ro_compat&0x0001 	 ) c<<"\tSparse superblocks and group descriptor tables "<<e;
        if(s_feature_ro_compat& 0x0002 	 ) c<<"\tFile system uses a 64-bit file size "<<e;
        if(s_feature_ro_compat& 0x0004 	) c<<"\tDirectory contents are stored in the form of a Binary Tree"<<e;
        c<<e;
    }
}

int Superblock::char4int(char *buf, int index)
{
    unsigned char q = buf[index];
    unsigned char w = buf[index+1];
    unsigned char r = buf[index+2];
    unsigned char t = buf[index+3];
    return (q + (w<<8) + (r<<16) + (t<<24));
}

int Superblock::char2int(char *buf, int index)
{
    unsigned char a = buf[index];
    unsigned char b = buf[index+1];
    return (a + (b<<8) );
}

